// pages/me/me.js
const app=getApp();
const get = require('../../utils/common.js')
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    page: 1,
    pages: 0,
    nomore: false,
    shareUserInfo: []
  },
  onLoad: function (options) {
    console.log(app.globalData.userInfo,this.data.shareUserInfo==false)
    let that =  this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        console.log(res)
        that.setData({
          openid : res.data 
        })
        console.log(that.data.openid)
        if(res.data){
        that.getShareUser()
        }
      }

    })
    wx.getStorage({
      key: 'avatarUrl',
      success(res){
        that.setData({
          avatarUrl : res.data 
        })
      }
    })
    wx.getStorage({
      key: 'nickName',
      success(res){
        that.setData({
          nickName : res.data 
        })
      }
    })
    wx.getStorage({
      key: 'telNum',
      success: function (res) {
        console.log(res.data)
        that.setData({telNum:res.data})
      }
    })
   setTimeout(() => {
    console.log(that.data.openid,that.data.avatarUrl)
    if (that.data.openid && that.data.avatarUrl) {
      that.setData({
        hasUserInfo: true
      })
    } 
   }, 500);

   
  },

  //获取用户信息
  // onGetUserInfo(e) {
  //   console.log("onGetUserInfo", e)
  //   let that = this;
  //   if (e.detail.errMsg = "getuserInfo:ok") {
  //     app.globalData.userInfo = e.detail.userInfo;
  //     that.setData({
  //       userInfo:e.detail.userInfo
  //     })
  //     //获取code
  //     wx.login({
  //       success(res) {
  //         console.log("wx.login", res);
  //         wx.showLoading({
  //           title: '登录中...',
  //          // mask: true
  //         });
  //         var code = res.code;
  //         that.getUserSessionKey(code)
  //       }
  //     })
  //   }
  // },
    //获取用户信息
    getUserProfile(e) {
      let that = this;
      wx.getUserProfile({
        desc: '用于完善会员资料', 
        success: (res) => {
          console.log(res)
        this.setData({
          nickName:res.userInfo.nickName,
          avatarUrl:res.userInfo.avatarUrl,
          userInfo:res.userInfo
        })
         //获取code
      wx.login({
        success(res) {
          console.log("wx.login", res);
          wx.showLoading({
            title: '登录中...',
           // mask: true
          });
          var code = res.code;
          that.getUserSessionKey(code)
        }
      })
        }
      })
    },
  //获取session_key和openid
  getUserSessionKey(code) {
    let that = this;
    get.getData('/useractiveprofession/getuserinfo',{code: code}).then(res=>{
      console.log("getUserSessionKey", res);
      var openid = res.data.data.openid
      var session_key = res.data.data.session_key
      app.globalData.openid = openid
      app.globalData.session_key = session_key
      console.log( openid,session_key)
      that.setData({
        openid:openid,
        session_key:session_key
      })
      console.log(that.data.openid)
      that.onLoginUser();
    }).catch(err=>{
    
    })
  },
  //onLoginUser
  onLoginUser(e) {
    var that=this
    var user = that.data.userInfo
    var openid = that.data.openid
    var session_key = that.data.session_key
    var shareUserInfo = that.data.shareUserInfo
    console.log(user)
    get.getData('/useractiveprofession/checkuser',{ openid: openid,session_key: session_key,nickname: user.nickName,headimgurl: user.avatarUrl,}).then(res=>{
      console.log("onLoginUser", res)
      console.log(that.data.userInfo)
      //没有成功分享
      if(!shareUserInfo){
        that.getShareUser()
      }
      wx.setStorage({
        key: 'nickName',
        data: that.data.userInfo.nickName
      });
      wx.setStorage({
        key: 'avatarUrl',
        data: that.data.userInfo.avatarUrl,
      });
      console.log(that.data.userInfo.avatarUrl,that.data.userInfo.nickName)
      //wx.hideLoading();
      wx.showToast({
        title: '登录成功',
        duration: 500
      });
  that.setData({
    hasUserInfo: true
  })
    }).catch(err=>{
      wx.showToast({
        title: '网络异常！err:authlogin',
        duration: 2000
      })
    })

  },
  getShareUser(){
    let that = this
    var page = that.data.page
    var nomore = that.data.nomore
    var shareUserInfo = that.data.shareUserInfo
    console.log(shareUserInfo)
    let limit = 14
    that.loading = true 
    console.log(that.data.openid,page)
    wx.request({
      url: `https://sim.xsmaofa.com/index/useractiveprofession/sharelists?page=${page}&limit=${limit}`,
      method:'post',
      data:{
        openid:that.data.openid
      },
      success(res){
        console.log(res.data,res.data.data,shareUserInfo)
        console.log(res.data.all)
        var shareInfoLength =res.data.all
        let pages = Math.ceil(shareInfoLength/limit)
        shareUserInfo = shareUserInfo.concat(res.data.data)
        that.setData({
          shareUserInfo,
          pages,
        })
        
          //that.loading = false
        console.log(that.data.pages,that.data.shareUserInfo)
      }
    })
  },
  onReachBottom() {
    console.log("daodile",this.data.page,this.data.pages)
    let nomore = this.data.nomore
    this.data.page >= this.data.pages?nomore==true:nomore==false
    if (this.data.page <= this.data.pages) {
      this.setData({
        page:this.data.page + 1,
        nomore
      })
      this.getShareUser()
      this.setData({nomore:nomore})
      console.log(nomore)
    }
    console.log(nomore)
  },
  freeTell(e){
    console.log(e.currentTarget.dataset.tel)
    var phoneNumber = e.currentTarget.dataset.tel
    wx.makePhoneCall({
      phoneNumber
    })
  },
  //进入收藏列表
  shouCang(res){
    console.log(res)
    let that = this
    var telNum = that.data.telNum
    var noShow = 1
    wx.navigateTo({
      url: '/pages/list/list?telNum=' + telNum + '&from_detail=' + noShow,
    })
  },
})